package me.itsoo.artemis.framework.core.generator;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.UUID;

/**
 * UUID 生成器
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/30
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UuidKeyGenerator implements KeyGenerator<String> {

    /**
     * 创建实例
     *
     * @return UuidKeyGenerator
     */
    public static UuidKeyGenerator create() {
        return new UuidKeyGenerator();
    }

    @Override
    public String next() {
        return createUuid(UUID.randomUUID().toString());
    }

    private String createUuid(String uuid) {
        final char[] dest = new char[32];
        copyArray(dest, uuid.toCharArray());
        return new String(dest);
    }

    private void copyArray(char[] dest, char[] src) {
        System.arraycopy(src, 0, dest, 0, 8);
        System.arraycopy(src, 9, dest, 8, 4);
        System.arraycopy(src, 14, dest, 12, 4);
        System.arraycopy(src, 19, dest, 16, 4);
        System.arraycopy(src, 24, dest, 20, 12);
    }
}
